package com.pac.hospital.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.common.tools.StringTools;
import com.common.util.ErrorNumber;
import com.common.util.LogicException;
import com.pac.hospital.bean.HospitalBean;
import com.pac.hospital.bean.HospitalManagerBean;
import com.pac.hospital.dao.HospitalDao;
import com.pac.hospital.service.HospitalManagerService;
import com.pac.hospital.service.HospitalService;

/**
 * 医院实现类
 * 
 * @author lujun.chen
 * @version [版本号, 2015年2月14日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service(value = "hospitalService")
public class HospitalServiceImpl implements HospitalService {

	private static Logger logger = Logger.getLogger(HospitalServiceImpl.class);

	@Autowired
	private HospitalDao hospitalDao;
	
	@Autowired
	private HospitalManagerService hospitalManagerService;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public String addHospital(HospitalBean hospitalBean) {

		if (null == hospitalBean) {
			logger.error("添加医院信息出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(hospitalBean.getName())) {
			logger.error("添加医院信息出错，医院名称为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "医院名称为空");
		}
		
		String hospitalId=hospitalDao.addHospital(hospitalBean);
		
		//添加医院管理员
		if(!StringTools.isNull(hospitalBean.getManagerUserId()))
		{
			HospitalManagerBean hospitalManagerBean=new HospitalManagerBean();
			hospitalManagerBean.setHospitalId(hospitalBean.getId());
			hospitalManagerBean.setUserId(hospitalBean.getManagerUserId());
			hospitalManagerService.addHospitalManager(hospitalManagerBean);
		}

		return hospitalId;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean updateHospital(HospitalBean hospitalBean) {
		if (null == hospitalBean) {
			logger.error("更新医院信息出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(hospitalBean.getId())) {
			logger.error("更新医院信息出错，医院ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "医院ID为空");
		}

		//修改医院管理员
		if(!StringTools.isNull(hospitalBean.getManagerUserId()))
		{
			HospitalManagerBean hospitalManagerBean=new HospitalManagerBean();
			hospitalManagerBean.setHospitalId(hospitalBean.getId());
			hospitalManagerService.delHospitalManager(hospitalManagerBean);
			
			hospitalManagerBean.setUserId(hospitalBean.getManagerUserId());
			hospitalManagerService.addHospitalManager(hospitalManagerBean);
		}
		
		return hospitalDao.updateHospital(hospitalBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean delHospitalById(String hospitalId) {
		if (StringTools.isNull(hospitalId)) {
			logger.error("按照医院ID删除医院信息出错，医院ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "医院ID为空");
		}
		HospitalBean hospitalBean = new HospitalBean();
		hospitalBean.setId(hospitalId);
		
		//删除医院管理员
		HospitalManagerBean hospitalManagerBean=new HospitalManagerBean();
		hospitalManagerBean.setHospitalId(hospitalBean.getId());
		hospitalManagerService.delHospitalManager(hospitalManagerBean);

		return hospitalDao.delHospital(hospitalBean);
	}

	@Override
	public List<HospitalBean> queryHospital(HospitalBean hospitalBean) {
		return hospitalDao.queryHospital(hospitalBean);
	}

	@Override
	public HospitalBean findHospital(HospitalBean hospitalBean) {

		List<HospitalBean> hospitalList = hospitalDao
				.queryHospital(hospitalBean);

		if (null != hospitalList && hospitalList.size() > 0) {
			return hospitalList.get(0);
		}

		return null;
	}

}
